package com.eshipenet.shipnet.edgeboxrpc.server;


import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.eshipenet.shipnet.edgebox.EdgeBox;
import com.eshipenet.shipnet.edgebox.EdgeBoxModel;
import com.eshipenet.shipnet.edgebox.EdgeBoxState;
import com.eshipenet.shipnet.edgeboxrpc.grpc.EdgeBoxModelRepoGrpcImpl;
import com.eshipenet.shipnet.edgeboxrpc.grpc.EdgeBoxRepoGrpcImpl;
import com.eshipenet.shipnet.edgeboxrpc.grpc.EdgeBoxStateRepoGrpcImpl;
import com.eshipenet.shipnet.edgeboxrpc.ignite.EdgeBoxIgnite;
import com.eshipenet.shipnet.edgeboxrpc.service.EdgeBoxIdGenerator;
import com.eshipenet.shipnet.edgeboxrpc.service.EdgeBoxIdGeneratorImpl;
import io.grpc.ServerBuilder;
import io.grpc.protobuf.services.ProtoReflectionService;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.CacheConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


class EdgeBoxStopActor extends AbstractActor {

  static Props props() {
    return Props.create(EdgeBoxStopActor.class, EdgeBoxStopActor::new);
  }



  @Override
  public void preStart() throws Exception {

    Ignite ignite = Ignition.ignite("EdgeBox");

    final IgniteCache<String, EdgeBox> edgeBoxCache = ignite.cache("EdgeBoxCache");
    final IgniteCache<String, EdgeBoxModel> edgeBoxModelCache = ignite.cache("EdgeBoxModelCache");


    CacheConfiguration<String, EdgeBoxState> stateCacheConfiguration =
      new CacheConfiguration<String, EdgeBoxState>("edgeboxstate");//动态缓存
    stateCacheConfiguration.setIndexedTypes(String.class, EdgeBoxState.class); // 注意配置注册key和value是为了sql查询
    final IgniteCache<String, EdgeBoxState> stateIgniteCache =
      ignite.getOrCreateCache(stateCacheConfiguration);//获取一个IgniteCache实例

    String edgeBoxStateIdGeneratorName = "EdgeBoxStateIdGenerator";
    ignite.services().deployClusterSingleton(edgeBoxStateIdGeneratorName, new EdgeBoxIdGeneratorImpl(1, 1));
    EdgeBoxIdGenerator edgeBoxStateIdGenerator = ignite.services().service(edgeBoxStateIdGeneratorName);

    String edgeBoxIdGeneratorName = "EdgeBoxIdGenerator";
    ignite.services().deployClusterSingleton(edgeBoxIdGeneratorName, new EdgeBoxIdGeneratorImpl(1, 1));
    EdgeBoxIdGenerator edgeBoxIdGenerator = ignite.services().service(edgeBoxIdGeneratorName);

    String edgeBoxModelGeneratorName = "EdgeBoxModelIdGenerator";
    ignite.services().deployClusterSingleton(edgeBoxModelGeneratorName, new EdgeBoxIdGeneratorImpl(1, 1));
    EdgeBoxIdGenerator edgeBoxModelIdGenerator = ignite.services().service(edgeBoxModelGeneratorName);

    ServerBuilder.forPort(50051)
                 .addService(new EdgeBoxStateRepoGrpcImpl(stateIgniteCache, edgeBoxStateIdGenerator))
                 .addService(new EdgeBoxRepoGrpcImpl(edgeBoxCache, edgeBoxIdGenerator))
                 .addService(new EdgeBoxModelRepoGrpcImpl(edgeBoxModelCache, edgeBoxModelIdGenerator))
                 .addService(ProtoReflectionService.newInstance())
                 .build()
                 .start();

  }

  @Override
  public void postStop() {
    Logger logger = LoggerFactory.getLogger("EdgeBoxActor");
    logger.debug("stop");
  }


  @Override
  public Receive createReceive() {
    return receiveBuilder().matchAny(t -> {
      getSender().tell(t, getSelf());
      //停止
      //getContext().stop(getSelf());
    }).build();

  }
}


public class EdgeBoxActor {

  public static void main(String[] args) throws java.io.IOException {

    //运行Ignite
    try {
      Logger logger = LoggerFactory.getLogger("EdgeBoxActor");
      logger.debug("start");
      EdgeBoxIgnite.ignite();
    } catch (Exception e) {
      e.printStackTrace();
    }

    ActorSystem system = ActorSystem.create("system");
    ActorRef first = system.actorOf(EdgeBoxStopActor.props(), "first");


  }

}